<?php 

class admin {
	var $module;
	var $modules;
	 
	function admin() {
		
		$this->pluginsdir = dirname(dirname(__FILE__))."/plugins/";
		$this->smarty = $_SESSION['joel']->newSmarty();
		
		// look for admin modules
		$this->getAdminModules();
	}
	
	function loadFrame () {
		$this->smarty->assign('modules',$this->modules);
		$this->smarty->display('admin.html');
	}
	
	function setModule ($obj) {
		$this->module = $obj->module;
		$_SESSION['mytm']['admin']['module'] = $this->module;
		
		$return = array();
		$return['data'] = array();
		$return['html'] = '';
		$return['class'] = '';
		
		$return['class'] = str_replace(array("\n","\t"),array("",""), file($this->pluginsdir."/".$obj->module."/class.js"));
		$return['html'] = str_replace(array("\n","\t"),array("",""), $this->smarty->fetch($this->pluginsdir."/".$obj->module."/".$obj->module.".html"));
		
		$thismodule = new $this->module();
		$thismodule->load();
		
		echo $this->json->encode($return);
	}
	
	function getAdminModules() {
		// fetch all admin_classes
		$d = dir($this->pluginsdir);
		while ($entry=$d->read()) {
			if (ereg("^admin_",$entry)) {
				$modulekey = substr ($entry, strpos($entry,"_")+1);
				require ($this->pluginsdir.$entry."/".$entry.".php");
				
				$thismodule = 'admin_'.$modulekey;
				$$thismodule = new $thismodule();
				$this->modules[$modulekey] = $$thismodule->register();
				$this->modules[$modulekey]['link'] = $modulekey;
				unset($module, $title, $prio);
			}
		}

		$d->close();
		return true;
	}
	
	function getModules () {
		global $db;
		$json = new Services_JSON();
		echo $json->encode($this->modules);
	}
}


?>